<template>
  <el-dialog :title="currentForm.carUuid ? '修改' : '添加'" :model-value="dialog" @close="close">
    <el-form :model="formData" :rules="rules" ref="uploadForm" label-width="auto" style="padding: 0 20px" class="Dform">
      <el-form-item label="品牌：" prop="brandUuid">
        <el-select v-model="formData.brandUuid" @change="getSeriesList(true)" filterable clearable style="width: 100%"
          placeholder="请选择品牌">
          <el-option v-for="item in brandList" :key="item.brandUuid" :label="item.brandName" :value="item.brandUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="车系：" prop="seriesUuid">
        <el-select v-model="formData.seriesUuid" @change="getModelList(true)" filterable clearable style="width: 100%"
          placeholder="请选择车系">
          <el-option v-for="item in seriesList" :key="item.seriesUuid" :label="item.seriesName"
            :value="item.seriesUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="车型：" prop="modelUuid">
        <el-select v-model="formData.modelUuid" filterable clearable style="width: 100%" placeholder="请选择车型">
          <el-option v-for="item in modelList" :key="item.modelUuid" :label="item.modelName" :value="item.modelUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号码：" prop="plateNumber">
        <el-input v-model="formData.plateNumber" />
      </el-form-item>
      <el-form-item label="车架号（vin）：" prop="vin">
        <el-input v-model="formData.vin" />
      </el-form-item>
      <el-form-item label="发动机号：" prop="engineNumber">
        <el-input v-model="formData.engineNumber" />
      </el-form-item>
      <el-form-item label="车辆所属方" prop="carOwner">
        <el-select v-model="formData.carOwner" filterable clearable style="width: 100%" placeholder="请选择车辆所属方">
          <el-option :label="item.partnerName" :value="item.partnerUuid" v-for="item in partnerList"
            :key="item.partnerUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆地址：" prop="cityCode">
        <area-code v-model:provinceCode="formData.provinceCode" v-model:cityCode="formData.cityCode"
          v-model:areaCode="formData.areaCode" :showArea="false" />
      </el-form-item>
      <!-- <el-form-item label="司机：" prop="driverUuid">
        <el-select v-model="formData.driverUuid" filterable clearable style="width: 100%" placeholder="请选择司机">
          <el-option :label="item.name" :value="item.driverUuid" v-for="item in driverList" :key="item.driverUuid" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button v-if="!formData.carUuid" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, PropType, watch, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { updateCar, saveCar, brandQueryAll, selectListBySeriesUuid, selectListByBrandUuid } from '@/api/vehicle'
import { partnerQuery } from "@/api/user"
import { validateCarNumber } from '@/utils'
import { driverQueryPage } from '@/api/system'
import AreaCode from '@/components/area-code/index.vue'

export default defineComponent({
  components: {
  },
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
    currentForm: {
      type: Object,
      default: () => { },
    },
    selectMenuList: {
      type: Array as PropType<{ title: string; id: string | number }[]>,
      default: () => [],
    },
  },
  emits: ['update:currentForm', 'update:dialog', 'refresh'],
  setup(props, context) {
    const formData = reactive(props.currentForm)
    const uploadForm = ref<any>(null)
    const brandList = ref<any>([])
    const seriesList = ref<any>([])
    const modelList = ref<any>([])
    const partnerList = ref<any>([])
    const driverList = ref<any>([])

    watch(() => props.dialog,
      (val) => {
        Object.assign(formData, props.currentForm)
      }
    )

    const close = () => {
      context.emit('update:dialog', false)
      context.emit('refresh', { ...formData })
      context.emit('update:currentForm', {})
    }
    const getSeriesList = (flag = false) => {
      if (flag) {
        formData.modelUuid = '';
      }
      if (!formData.brandUuid) {
        return
      }
      selectListByBrandUuid({ brandUuid: formData.brandUuid, pageNum: 1, pageSize: 100 }).then((res: any) => {
        seriesList.value = res.data || []
      })
    }
    const getModelList = (flag = false) => {
      if (flag) {
        formData.modelUuid = '';
      }
      if (!formData.seriesUuid) {
        return
      }
      selectListBySeriesUuid({ seriesUuid: formData.seriesUuid, pageNum: 1, pageSize: 100 }).then((res: any) => {
        modelList.value = res.data || []
      })
    }

    onMounted(() => {
      brandQueryAll().then((res: any) => {
        brandList.value = res.data || []
      })
      if (formData.brandUuid) {
        getSeriesList()
      }
      console.log(formData)

      // getSeriesList()
      if (formData.seriesUuid) {
        getModelList()
      }
      // getSeriesList()
      // getModelList()
      partnerQuery({ pageNum: 1, pageSize: 1000 }).then((res: any) => {
        partnerList.value = res.data.list || []
      })
      // driverQueryPage({ pageNum: 1, pageSize: 1000 }).then((res: any) => {
      //   driverList.value = res.data.list || []
      // })
    })

    const onSubmit = () => {
      uploadForm.value.validate((valid: boolean) => {
        if (valid) {
          if (formData.carUuid) {
            updateCar(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('修改成功')
                close()
              }
            })
          } else {
            saveCar(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('添加成功')
                close()
              }
            })
          }
        } else {
          ElMessage({
            message: '验证失败',
            type: 'error',
            duration: 1000,
          })
          return false
        }
      })
    }

    const resetForm = () => {
      uploadForm.value.resetFields()
      context.emit('update:currentForm', {})
    }

    return {
      uploadForm,
      formData,
      brandList,
      seriesList,
      modelList,
      partnerList,
      driverList,
      close,
      resetForm,
      onSubmit,
      getSeriesList,
      getModelList,
      rules: {
        brandUuid: [
          { required: true, message: '请选择品牌', trigger: 'blur' }
        ],
        seriesUuid: [
          { required: true, message: '请选择车系', trigger: 'blur' }
        ],
        modelUuid: [
          { required: true, message: '请选择车型', trigger: 'blur' }
        ],
        plateNumber: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          { validator: validateCarNumber }
        ],
        vin: [
          { required: true, message: '请输入车架号（vin）', trigger: 'blur' }
        ],
        engineNumber: [
          { required: true, message: '请输入发动机号', trigger: 'blur' }
        ],
        carOwner: [
          { required: true, message: '请选择车辆所属方', trigger: 'blur' }
        ],
        cityCode: [
          { required: true, message: '请选择城市', trigger: 'blur' }
        ],
      },
    }
  },
})
</script>

<style></style>
